package ExpressOne;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*
    内存显示
 */

public class MemoryDisplay extends JFrame {
    JButton[] buttons;
    private final int MaxSize;

    MemoryDisplay(int MaxSize) {
        super("内存示意图");
        this.MaxSize = MaxSize;
        setLayout(new GridLayout(4, MaxSize / 4));

        buttons = new JButton[MaxSize];
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton();
            buttons[i].setBackground(Color.green);
            add(buttons[i]);
        }
        JMenuBar bar = new JMenuBar();
        JMenu menu = new JMenu("帮助");
        JMenuItem[] item = new JMenuItem[4];
        item[0] = new JMenuItem(" 红色 : 阻塞 ");
        item[1] = new JMenuItem(" 绿色 : 空闲 ");
        item[2] = new JMenuItem(" 黑色 : 就绪 ");
        item[3] = new JMenuItem(" 粉色 : 运行 ");
        for (JMenuItem i : item) {
            menu.add(i);
        }
        bar.add(menu);
        setJMenuBar(bar);
        //setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        setVisible(true);
        JOptionPane.showMessageDialog(this, "红色代表:阻塞\n绿色代表:空闲\n黑色代表:就绪\n粉色代表:运行\n"
                , "tips:提示", JOptionPane.INFORMATION_MESSAGE);
    }

    private void setColor(int begin, int end, Color color) {
        for (int i = begin; i < end; i++) {
            buttons[i].setBackground(color);
        }
    }

    void setRedColor(int begin, int end) {
        this.setColor(begin, end, Color.RED);
    }

    void setGreenColor(int begin, int end) {
        this.setColor(begin, end, Color.GREEN);
    }

    void setBlackColor(int begin, int end) {
        this.setColor(begin, end, Color.BLACK);
    }

    void setPinkColor(int begin, int end) {
        this.setColor(begin, end, Color.PINK);
    }
}
